package com.vlong.town.util.date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;

/**
 * 日期处理工具类
 * @author Assassin
 */
public class DateUtil {
	

    // 设置时间格式
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    
    
    /**
     *  根据当前日期获取前一天的日期
     * @param newDate
     * @return
     */
    public static Date getYesterdayDate(Date newDate) {
    	Calendar ca = Calendar.getInstance();//得到一个Calendar的实例 
    	ca.setTime(newDate); //设置时间为当前时间 
        ca.add(Calendar.DATE, -1);//得到前一天的日期
        Date date = ca.getTime();
		return date;
    }
    
    /**
     * 根据日期计算所在周的第一天与最后一天
     * @param date 日期
     */
    public static Map<String, Object> getFirstAndLastOfWeek(Date date) {
        Map<String, Object> map = new HashMap<>();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        int day = cal.get(Calendar.DAY_OF_WEEK);
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        String imptimeBegin = sdf.format(cal.getTime());
        cal.add(Calendar.DATE, 6);
        String imptimeEnd = sdf.format(cal.getTime());
        map.put("first", imptimeBegin);
        map.put("last", imptimeEnd);
        return map;
    }

    /**
     * 根据日期计算当月的第一天与最后一天
     * @param date 日期
     * @return
     */
    public static Map<String, Object> getFirstAndLastOfMonth(Date date) {
        Map<String, Object> map = new HashMap<>();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        Date theDate = calendar.getTime();
        // 月第一天
        GregorianCalendar gcLast = (GregorianCalendar) Calendar.getInstance();
        gcLast.setTime(theDate);
        gcLast.set(Calendar.DAY_OF_MONTH, 1);
        String day_first = sdf.format(gcLast.getTime());
        // 月最后一天
        calendar.add(Calendar.MONTH, 1);
        calendar.set(Calendar.DATE, 1);
        calendar.add(Calendar.DATE, -1);
        String day_last = sdf.format(calendar.getTime());
        map.put("first", day_first);
        map.put("last", day_last);
        return map;

    }

    /**
     * 根据日期计算当年的第一天与最后一天
     * @param date 日期
     * @return
     */
    public static Map<String, Object> getFirstAndLastOfYear(Date date) {
        Map<String, Object> map = new HashMap<>();
        Calendar calendar = Calendar.getInstance();
        int year = date.getYear() + 1900;
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        Date currYearFirst = calendar.getTime();
        calendar.set(Calendar.YEAR, year + 1);
        calendar.add(calendar.DATE, -1);
        Date lastYearFirst = calendar.getTime();
        map.put("first", sdf.format(currYearFirst));
        map.put("last", sdf.format(lastYearFirst));
        return map;

    }

    /**
     * 计算两个日期之间相差的天数
     * @param early 日期-前
     * @param late 日期-后
     * @return days
     */
    public static int getDaysBetween(Date early, Date late) {
        Calendar calst = Calendar.getInstance();
        Calendar caled = Calendar.getInstance();
        calst.setTime(early);
        caled.setTime(late);
        //设置时间为0时
        calst.set(Calendar.HOUR_OF_DAY, 0);
        calst.set(Calendar.MINUTE, 0);
        calst.set(Calendar.SECOND, 0);
        caled.set(Calendar.HOUR_OF_DAY, 0);
        caled.set(Calendar.MINUTE, 0);
        caled.set(Calendar.SECOND, 0);
        //得到两个日期相差的天数
        int days = ((int) (caled.getTime().getTime() / 1000) - (int) (calst.getTime().getTime() / 1000)) / 3600 / 24;
        return days;
    }

    /**
     * 计算两个日期之间相差的月数
     * @param early 日期-前
     * @param late 日期-后
     * @return months
     */
    public static int getMonthsBetween(Date early, Date late) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Calendar bef = Calendar.getInstance();
        Calendar aft = Calendar.getInstance();
        bef.setTime(early);
        aft.setTime(late);
        int result = aft.get(Calendar.MONTH) - bef.get(Calendar.MONTH);
        int month = (aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR)) * 12;
        return Math.abs(month + result);
    }

    /**
     * 计算两个日期之间相差的年数
     * @param early 日期-前
     * @param late 日期-后
     * @return years
     */
    public static int getYearsBetween(Date early, Date late) {
        Calendar calBegin = Calendar.getInstance();
        Calendar calEnd = Calendar.getInstance();
        calBegin.setTime(early);
        calEnd.setTime(late);
        return calEnd.get(Calendar.YEAR) - calBegin.get(Calendar.YEAR);
    }
    
	/**
	 * 根据当前时间算出天数
	 * @param time
	 * @return
	 */
	public static Long getDate2long(Long time){
		return time/24/60/60/1000;
	}
	
	/** 
	* 字符串转换成日期 
	* @param str 
	* @return date 
	*/ 
	public static Date strToDate(String str,String pattern) { 
	   SimpleDateFormat format = new SimpleDateFormat(pattern); 
	   Date date = null; 
	   try { 
	    date = format.parse(str); 
	   } catch (ParseException e) { 
	    e.printStackTrace(); 
	   } 
	   return date; 
	} 
	
	/** 
	* 日期转换成字符串 
	* @param date 
	* @return str 
	*/ 
	public static String dateToStr(Date date,String pattern) {
	   SimpleDateFormat format = new SimpleDateFormat(pattern); 
	   String str = format.format(date); 
	   return str; 
	} 
	
	
	/** 
	* 字符串转换成时间戳
	* @param str 
	* @return date 
	*/ 
	public static Long strToLong(String str,String pattern) {
	   SimpleDateFormat format = new SimpleDateFormat(pattern); 
	   Long dateLong = null; 
	   try { 
		  dateLong=format.parse(str).getTime(); 
	   } catch (ParseException e) { 
	    e.printStackTrace(); 
	   } 
	   return dateLong; 
	}
	
	public static void main(String[] args) {
		Long startTime = strToLong("2018-06-10","yyyy-MM-dd");
		Long endTime = strToLong("2018-06-04","yyyy-MM-dd");
		
		
		/*Calendar  calendar = Calendar.getInstance();
		Date date=new  Date(endTime);
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, 31);
		calendar.getTimeInMillis();
		System.out.println(calendar.getTimeInMillis());*/
		
		
		Long date2long = getDate2long(startTime);
		Long datelong = getDate2long(System.currentTimeMillis());
		System.out.println(datelong-date2long);
	}
	
	/**
	 * 得到日期时间字符串，转换格式（yyyy-MM-dd HH:mm:ss）
	 */
	public static String formatDateTime(Long date) {
		SimpleDateFormat sdf1 =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//这个是你要转成后的时间的格式
        String sd ="";
        if (date!=null){
           sd = sdf1.format(new Date(Long.parseLong(String.valueOf(date))));   // 时间戳转换成时间
        }
		return sd;
	}
	                                 
	
}
